﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASKM.Rachao.Model.Dto;
using ASKM.Rachao.Model.Assembler;
using ASKM.Rachao.Model.Extensions;
using System.Transactions;

namespace ASKM.Rachao.Model.Facade
{
    public class Profile: BaseFacade
    {

        public Profile()
        {

        }
        
        /// <summary>
        /// Initial user register
        /// </summary>
        /// <param name="dto">InitialRegisterDto</param>
        /// <returns>
        /// Player profile id
        /// </returns>
        public int InitialRegister(InitialRegisterDto dto)
        {
            try
            {
                using (var domain = new Domain.Profile())
                {
                    PlayerProfile pp = ProfileAssembler.ConvertInitialRegisterDtoToPlayerProfile(dto);
                    UserAccessProfile uap = ProfileAssembler.ConvertInitialRegisterDtoToUserAccessProfile(dto);
                    pp.UserAccessProfile.Add(uap);

                    using (var scope = new TransactionScope())
                    {
                        var ret = domain.InitialRegister(pp);
                        scope.Complete();
                        return ret;
                    }
                }


            }

            catch (Rachao.Model.Exceptions.RachaoBusinessException rbe)
            {
                throw rbe;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Int32 ReleaseTicket(string ticket)
        {
            using (var security = new Domain.Security())
            using (var domain = new Domain.Profile())
            {
                var ppat = security.GetTicketFromString(ticket);
                bool isApproved = security.GetApprovalStatusFromTicketString(ticket);
                domain.ChangeProfileStatusByTicket(ppat, isApproved);
                return ppat.PlayerProfileId;
            }
        }        

    }
}
